package org.opens.config;

import org.opens.entity.City3;
import org.opens.entity.City3User;
import org.opens.entity.City4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * 说明:
 * - @ConditionalOnBean(name = "city3")也可以放在类上
 */
@Configuration
//@ConditionalOnBean(name = "city3")
public class ConditionalOnBeanConfig {

    /**
     * 功能: `@ConditionalOnBean(name = "city3")`表示当容器中有对应的bean时才将当前方法返回的bean初始化到ioc容器中.
     * @param city3 自动装配的bean
     * @return 初始化的bean
     */
    @Bean
    @ConditionalOnBean(name = "city3")
    public City3User city3User(@Autowired City3 city3) {
        City3User city3User = new City3User();
        city3User.setAge(13);
        city3User.setName("s13");
        city3User.setId(42324);
        city3User.setCity3(city3);
        return city3User;
    }

    /**
     * 功能: `@ConditionalOnMissingBean(name = "city3")`表示当ioc容器中没有指定的bean时才初始化当前方法返回的bean到ioc容器中.
     * @return 初始化的bean
     */
    @Bean
    @ConditionalOnMissingBean(name = "city3")
    public City4 city4() {
        City4 city4 = new City4();
        city4.setId("2323");
        city4.setName("s2asdc");
        return city4;
    }

}
